import requests
import json
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt

class handle:
    def __init__(self):
        self._base_url = "https://www.yixianshenji.com/"
        # self._base_url = "http://120.78.167.177:5000/"

    def _getFile(self, file_path_list):
        temp = dict()
        for index in range(file_path_list.__len__()):
            temp[str(index)] = open(file_path_list[index], "rb")
        return temp

    def _handle(self, url, htype="POST", data=None, files=None):
        state = True
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        try:
            if data is not None and files is not None:
                if htype == "POST":
                    res = requests.post(url=url, data=data, files=files)
                elif htype == "GET":
                    res = requests.get(url=url, data=data, files=files)
                ret = json.loads(res.content)
                ret = ret.get("response")[0]
            elif data is not None and files is None:
                if htype == "POST":
                    res = requests.post(url=url, data=data)
                elif htype == "GET":
                    res = requests.get(url=url, data=data)
                ret = json.loads(res.content)
            elif data is None and files is not None:
                if htype == "POST":
                    res = requests.post(url=url, files=files)
                elif htype == "GET":
                    res = requests.get(url=url, files=files)
                ret = json.loads(res.content)
            else:
                if htype == "POST":
                    res = requests.post(url=url)
                elif htype == "GET":
                    res = requests.get(url=url)
                ret = json.loads(res.content)

            if ret.get("error") is not None:
                state = False
        except Exception as e:
            print(e)
            ret = None
            state = False
        QApplication.restoreOverrideCursor()

        return state, ret
